package com.xywl.escort.view.market.skus;

import android.view.View;

import com.xywl.escort.widget.flowlayout.FlowLayout;
import com.xywl.escort.widget.flowlayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * sku观察者回调类
 */
public class ItemClickListener implements TagFlowLayout.OnTagClickListener {

    private UiData mUiData;
    private SkuAdapter currentAdapter;

    public ItemClickListener(UiData uiData, SkuAdapter currentAdapter) {
        mUiData = uiData;
        this.currentAdapter = currentAdapter;
    }

    @Override
    public boolean onTagClick(View view, int position, FlowLayout parent) {
        if (currentAdapter.getAttributeMembersEntities().get(position).status == 2) {
            return true;
        }
        // 设置当前单选点击
        for (ProductModel.AttributesEntity.AttributeMembersEntity entity : currentAdapter.getAttributeMembersEntities()) {
            if (entity.equals(currentAdapter.getAttributeMembersEntities().get(position))) {
                if (entity.status == 0) {
                    entity.status = 1;
                    // 添加已经选择的对象
                    currentAdapter.setCurrentSelectedItem(entity);
                } else {
                    // 如果之前已经选中，则取消该按钮（删除节点）
                    currentAdapter.setCurrentSelectedItem(null);
                }
            } else {
                entity.status = entity.status == 2 ? 2 : 0;
            }
        }
        // 存放当前被点击的按钮
        mUiData.selectedEntities.clear();
        for (int i = 0; i < mUiData.adapters.size(); i++) {
            if (mUiData.adapters.get(i).getCurrentSelectedItem() != null) {
                mUiData.selectedEntities.add(mUiData.adapters.get(i).getCurrentSelectedItem());
            }
        }
        // 处理未选中的按钮
        for (int i = 0; i < mUiData.adapters.size(); i++) {
            for (ProductModel.AttributesEntity.AttributeMembersEntity entity : mUiData.adapters.get(i).getAttributeMembersEntities()) {
                // 处理没有数据和没有库存(检测单个)
                if (mUiData.result.get(entity.attributeMemberId + "") == null ||
                        mUiData.result.get(entity.attributeMemberId + "").stock <= 0) {
                    entity.status = 2;
                } else if (entity.equals(mUiData.adapters.get(i).getCurrentSelectedItem())) {
                    entity.status = 1;
                } else {
                    entity.status = 0;
                }
                // 冒泡排序
                List<ProductModel.AttributesEntity.AttributeMembersEntity> cacheSelected = new ArrayList<>();
                cacheSelected.add(entity);
                cacheSelected.addAll(mUiData.selectedEntities);
                for (int j = 0; j < cacheSelected.size() - 1; j++) {
                    for (int k = 0; k < cacheSelected.size() - 1 - j; k++) {
                        ProductModel.AttributesEntity.AttributeMembersEntity cacheEntity;
                        if (cacheSelected.get(k).attributeGroupId > cacheSelected.get(k + 1).attributeGroupId) {
                            // 交换数据
                            cacheEntity = cacheSelected.get(k);
                            cacheSelected.set(k, cacheSelected.get(k + 1));
                            cacheSelected.set(k + 1, cacheEntity);
                        }
                    }
                }
                for (int j = 0; j < cacheSelected.size() - 1; j++) {
                    for (int k = 0; k < cacheSelected.size() - 1 - j; k++) {
                        if (cacheSelected.get(k).attributeGroupId == cacheSelected.get(k + 1).attributeGroupId) {
                            cacheSelected.remove(k + 1);
                        }
                    }
                }
                StringBuilder stringBuilder = new StringBuilder();
                for (ProductModel.AttributesEntity.AttributeMembersEntity selectedEntity : cacheSelected) {
                    stringBuilder.append(selectedEntity.attributeMemberId);
                    stringBuilder.append(";");
                }
                // TODO 检查数据
                if (mUiData.result.get(stringBuilder.substring(0, stringBuilder.length() - 1)) != null && mUiData.result.get(stringBuilder.substring(0, stringBuilder.length() - 1)).stock > 0) {
                    entity.status = entity.status == 1 ? 1 : 0;
                } else {
                    entity.status = 2;
                }
            }
            mUiData.adapters.get(i).notifyDataChanged();
        }
        // TODO 设置展示的数据
        SkuUtil.setBabyShowData(mUiData);
        return true;
    }
}
